window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		U.fillHead();
		U.fillFooter();
		var id = U.getParam('id');
		initPage();

		function initPage() { //初始化页面
			U.loadOpt(false);
			get_donate();
			initEvent();
		}

		function initEvent() {
			$('.support span').click(function(){
				location.href='donation_pay.html?id='+id;
			});
			$('.menu li').width(($('.menu').width() - 2) / 3);
			$('.menu li').click(function() {
				var index = $(this).index();
				$('.donate_detail,.project_progress,.supporter_list').hide();
				$('.menu li').css({ 'background': 'white', 'color': 'black' });
				$(this).css({ 'background': 'red', 'color': 'white' });
				if(index == 0) {
					$('.donate_detail').show();
				}
				if(index == 1) {
					$('.project_progress').show();
					get_progress();
				}
				if(index == 2) {
					$('.supporter_list').show();
					get_supportList();
				}
			});
		}

		function get_donate() {
			U.ajaxget('/crowd/edit/' + id, {}, function(json) {
				if(json.success) {
					var result = json.models.crowdVO;
					var state = result.state;
					if(state == 1) { // 2.weizhankai.png 3.xiangmujieshu.png  1.jingxinzhong.png
						state = '未开始';
					} else if(state == 2) {
						state = '进行中';
					} else {
						state = '已结束';
					}
					var days = U.getNum(U.getMyDate(result.endTime));
					var money = result.totalAmount / result.amount * 100;
					var temImg = '';
					if(U.getImg(result.images).length > 0) {
						temImg = U.getImg(result.images)[0];
					}
					$('.img img').attr('src', temImg);
					$('.title>span:first-child').text(result.title);
					$('.title>span:last-child').text(state);
					$('.money').text('￥' + result.totalAmount);
					$('.total').text(result.amount);
					$('.d_time').text(U.getMyDate(result.beginTime));
					$('.state_img').width(money + '%');
					$('.day').text(days);
					$('.contents').text(result.intro);
					$('.donate_detail').show();
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}

		function get_progress() { //获得进展
			$('.project_progress').html('');
			var paramStr = {
				crowdId: id,
				curPage: 1,
				pageSize: 10
			}
			U.ajax('/progress/list', paramStr, function(json) {
				if(json.success) {
					var result = json.models.progressList;
					$(result).each(function() {
						var imglist = '';
						$(U.getImg(this.imageList)).each(function() {
							imglist += this;
						});
						$('.project_progress').append('<div class="progress"><div class="left">' + U.getMyDate(this.gmtCreate) + '</div><div class="right"><span></span><div>' + this.intro + '</div><div class="img_list">' + imglist + '</div></div></div>');

					});

				} else {
					M.toast(false, json.errorMsg);
				}

			});
		}
		function get_supportList() { //获得支持者
			$('.supporter_list').html('');
			var paramStr = {
				crowdId: id,
				state:2,
				curPage: 1
			}
			U.ajax('/support/list', paramStr, function(json) {
				if(json.success) {
					var result = json.models.supportRecordList;
					$(result).each(function() {
						$('.supporter_list').prepend('<div class="supporter"><img src="' + this.images + '"><ul><li>' + this.username + '支持了' + this.amount/100.0 + '元</li><li>寄语：' + this.message + '</li></ul><span class="date">2017-2-17</span></div>');
					});
				} else {
					M.toast(false, json.errorMsg);
				}

			});

		}

	});
}